package in.muton.edushare.newentities;

import in.muton.edushare.auth.roles.UnivRoleCheck;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.model.IModel;

import com.edushare.common.dao.GroupsMembersDAO;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.Groups;
import com.edushare.common.vo.Users;

public class GroupUserCreator extends UserInviter {

	private IModel<Groups> gmodel;
	private int userRole;

	public GroupUserCreator(ORMInterface ormInterface, IModel<Groups> gmodel, int userRole) {
		super(ormInterface);
		this.ormInterface = ormInterface;
		this.gmodel = gmodel;
		this.userRole = userRole;
	}
	
	public List<String> addUsers(String csvUserID){
		List<String> feedback = new ArrayList<String>();
		String[] userIds = UserInviter.breakString(csvUserID);
		for(String userId: userIds){
			Users u = super.getUserByString(userId);
			UnivRoleCheck urc = new UnivRoleCheck(gmodel.getObject().getUniversity(), u);
			if(		u!=null && 
					(urc.isMember()||urc.isInvited())	
			){
					if(u.isGroupMember(gmodel.getObject())){
						feedback.add(userId + " already exists in this group.");
					}else{
						new GroupsMembersDAO(ormInterface).addNewMember(gmodel.getObject(), u, userRole);
						feedback.add("Invitation is  sent to " + userId);
					}
			}else{
				feedback.add(userId + " cannot be added. Please make sure the user is a university member.");
			}
		}
		
		return feedback;
	}

}
